<template>
  <div class="mod-config">
    <el-form>
      <el-form-item>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('purchase:purchaseacceptresult:save')" type="primary"
            @click="addOrUpdateHandle()">新增</el-button>
          <el-button size="mini" v-if="isAuth('purchase:purchaseacceptresult:delete')" type="danger"
            @click="deleteHandle()" :disabled="dataListSelections.length <= 0">删除</el-button>
        </el-button-group>
        <el-button-group>
          <el-button size="mini" v-if="isAuth('purchase:purchaseacceptresult:export')" type="primary"
            @click="exportData">导出</el-button>
          <el-button size="mini" v-if="isAuth('purchase:purchaseacceptresult:import')" type="primary"
            @click="importData()">导入</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" size="mini" placeholder="参数名" clearable @clear="getDataList()"
          @blur="getDataList()"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="success" @click="getDataList()">查询</el-button>
        <el-button @click="resetForm" size="mini" type="info">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border size="mini" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" @cell-click="tableDbEdit" @cell-dblclick="tableDbEdit"
      :header-cell-style="{ background: '#fcfcfc', color: '#606266', height:'36px'}">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column type="index" header-align="center" align="center" label="#" width="40">
      </el-table-column>
      <el-table-column prop="resultId" header-align="center" align="center" label="">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.resultId" v-if="showInput == `resultId${scope.row.Id}`"
            @blur='blurInput(scope.row, "resultId", scope.row.resultId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.resultId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="acceptId" header-align="center" align="center" label="验收单id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.acceptId" v-if="showInput == `acceptId${scope.row.Id}`"
            @blur='blurInput(scope.row, "acceptId", scope.row.acceptId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.acceptId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="acceptNo" header-align="center" align="center" label="验收单号">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.acceptNo" v-if="showInput == `acceptNo${scope.row.Id}`"
            @blur='blurInput(scope.row, "acceptNo", scope.row.acceptNo)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.acceptNo}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="itmeId" header-align="center" align="center" label="验收单明细Id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.itmeId" v-if="showInput == `itmeId${scope.row.Id}`"
            @blur='blurInput(scope.row, "itmeId", scope.row.itmeId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.itmeId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsId" header-align="center" align="center" label="药品Id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.goodsId" v-if="showInput == `goodsId${scope.row.Id}`"
            @blur='blurInput(scope.row, "goodsId", scope.row.goodsId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.goodsId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="samplingNum" header-align="center" align="center" label="抽样数量">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.samplingNum" v-if="showInput == `samplingNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "samplingNum", scope.row.samplingNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.samplingNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="samplingOkNum" header-align="center" align="center" label="抽样合格数量">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.samplingOkNum" v-if="showInput == `samplingOkNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "samplingOkNum", scope.row.samplingOkNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.samplingOkNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isSamplingView" header-align="center" align="center" label="抽样外观 1合格，0不合格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.isSamplingView" v-if="showInput == `isSamplingView${scope.row.Id}`"
            @blur='blurInput(scope.row, "isSamplingView", scope.row.isSamplingView)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.isSamplingView}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isSamplingPackage" header-align="center" align="center" label="抽样包装 1合格，0不合格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.isSamplingPackage"
            v-if="showInput == `isSamplingPackage${scope.row.Id}`"
            @blur='blurInput(scope.row, "isSamplingPackage", scope.row.isSamplingPackage)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.isSamplingPackage}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isSamplingLabel" header-align="center" align="center" label="抽样标签 1合格，0不合格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.isSamplingLabel" v-if="showInput == `isSamplingLabel${scope.row.Id}`"
            @blur='blurInput(scope.row, "isSamplingLabel", scope.row.isSamplingLabel)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.isSamplingLabel}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="hasSamplingInstruction" header-align="center" align="center" label="抽样标签 1有，0无">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.hasSamplingInstruction"
            v-if="showInput == `hasSamplingInstruction${scope.row.Id}`"
            @blur='blurInput(scope.row, "hasSamplingInstruction", scope.row.hasSamplingInstruction)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.hasSamplingInstruction}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="hasQualityCertificate" header-align="center" align="center" label="合格证 1有，0无">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.hasQualityCertificate"
            v-if="showInput == `hasQualityCertificate${scope.row.Id}`" @blur='blurInput(scope.row, "hasQualityCertificate", 
          scope.row.hasQualityCertificate)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.hasQualityCertificate}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="aacceptedQualityNum" header-align="center" align="center" label="验收合格数量">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.aacceptedQualityNum"
            v-if="showInput == `aacceptedQualityNum${scope.row.Id}`"
            @blur='blurInput(scope.row, "aacceptedQualityNum", scope.row.aacceptedQualityNum)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.aacceptedQualityNum}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="hasAacceptedReport" header-align="center" align="center" label="检验报告书 1有，0无">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.hasAacceptedReport"
            v-if="showInput == `hasAacceptedReport${scope.row.Id}`"
            @blur='blurInput(scope.row, "hasAacceptedReport", scope.row.hasAacceptedReport)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.hasAacceptedReport}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="qualityCondition" header-align="center" align="center" label="质量状况 1合格，0不合格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.qualityCondition"
            v-if="showInput == `qualityCondition${scope.row.Id}`"
            @blur='blurInput(scope.row, "qualityCondition", scope.row.qualityCondition)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.qualityCondition}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isAacceptedResult" header-align="center" align="center" label="验收结论 1合格，0不合格">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.isAacceptedResult"
            v-if="showInput == `isAacceptedResult${scope.row.Id}`"
            @blur='blurInput(scope.row, "isAacceptedResult", scope.row.isAacceptedResult)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.isAacceptedResult}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="aacceptedUserId" header-align="center" align="center" label="验收人Id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.aacceptedUserId" v-if="showInput == `aacceptedUserId${scope.row.Id}`"
            @blur='blurInput(scope.row, "aacceptedUserId", scope.row.aacceptedUserId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.aacceptedUserId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="aacceptedUsername" header-align="center" align="center" label="验收人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.aacceptedUsername"
            v-if="showInput == `aacceptedUsername${scope.row.Id}`"
            @blur='blurInput(scope.row, "aacceptedUsername", scope.row.aacceptedUsername)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.aacceptedUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="signPic" header-align="center" align="center" label="签字图片">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.signPic" v-if="showInput == `signPic${scope.row.Id}`"
            @blur='blurInput(scope.row, "signPic", scope.row.signPic)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.signPic}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="photoPic" header-align="center" align="center" label="牌照图片">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.photoPic" v-if="showInput == `photoPic${scope.row.Id}`"
            @blur='blurInput(scope.row, "photoPic", scope.row.photoPic)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.photoPic}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="uploadPic" header-align="center" align="center" label="本地上传图片">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.uploadPic" v-if="showInput == `uploadPic${scope.row.Id}`"
            @blur='blurInput(scope.row, "uploadPic", scope.row.uploadPic)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.uploadPic}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeId" header-align="center" align="center" label="门店id">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeId" v-if="showInput == `storeId${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeId", scope.row.storeId)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeId}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeName" header-align="center" align="center" label="门店名称">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeName" v-if="showInput == `storeName${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeName", scope.row.storeName)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeCode" header-align="center" align="center" label="门店编码">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.storeCode" v-if="showInput == `storeCode${scope.row.Id}`"
            @blur='blurInput(scope.row, "storeCode", scope.row.storeCode)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.storeCode}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.createTime" v-if="showInput == `createTime${scope.row.Id}`"
            @blur='blurInput(scope.row, "createTime", scope.row.createTime)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="修改时间">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.updateTime" v-if="showInput == `updateTime${scope.row.Id}`"
            @blur='blurInput(scope.row, "updateTime", scope.row.updateTime)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createUsername" header-align="center" align="center" label="创建人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.createUsername" v-if="showInput == `createUsername${scope.row.Id}`"
            @blur='blurInput(scope.row, "createUsername", scope.row.createUsername)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.createUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateUsername" header-align="center" align="center" label="修改人">
        <template slot-scope="scope">
          <el-input size="mini" v-model="scope.row.updateUsername" v-if="showInput == `updateUsername${scope.row.Id}`"
            @blur='blurInput(scope.row, "updateUsername", scope.row.updateUsername)' v-focus>
          </el-input>
          <span v-else style="text-decoration:none;">{{scope.row.updateUsername}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="是否可用 1:是">
        <template slot-scope="scope">
          <el-switch size="mini" v-model="scope.row.status" @change="saveHandle(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注">
        <template slot-scope="scope">
          <span v-html="scope.row.remark"></span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseacceptresult:save')"
            @click="saveHandle(scope.row)">保存</el-button>
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseacceptresult:update')"
            @click="addOrUpdateHandle(scope.row.resultId)">修改</el-button>
          <el-button size="mini" type="text" v-if="isAuth('purchase:purchaseacceptresult:delete')"
            @click="deleteHandle(scope.row.resultId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './purchaseacceptresult-add-or-update'
  import {
    downLoadFile
  } from '@/utils/myUtil.js'
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        showInput: "",
        oldData: {},
        currentData: {},
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      async getDataList() {
        this.dataListLoading = true
        const res = await this.$http({
          url: `/purchase/purchaseacceptresult/list`,
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'dataForm': this.dataForm
          })
        })
        let data = res.data
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
          this.dataList.forEach(item => {
            item.status = item.status == 1 ? true : false
          })
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.resultId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$http({
            url: `/purchase/purchaseacceptresult/delete`,
            method: 'post',
            data: this.$http.adornData(ids, false)
          })
          let data = res.data
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      resetForm() {
        this.$nextTick(() => {
          this.dataForm = this.$options.data().dataForm
          this.getDataList()
        })
      },
      async saveHandle(row) {
        row.status = row.status ? 1 : 0
        const res = await this.$http({
          url: `/purchase/purchaseacceptresult/update`,
          method: 'post',
          data: this.$http.adornData(row)
        });
        let data = res.data
        if (data && data.code !== 0) {
          row.status = !row.status;
          return this.$message.error('修改失败!');
        }
        this.$message.success('更新成功！');
        this.getDataList();
      },
      // 当input失去光标后进行的操作
      async blurInput(row, name, value) {
        // 判断数据是否有所改变，如果数据有改变则调用修改接口
        if (this.oldData[name] != value) {
          row[name] = value
          if (value === true) {
            row[name] = 1
          } else if (value === false) {
            row[name] = 0
          }
        }
        this.showInput = ""
        this.currentData = row
      },
      tableDbEdit(row, column, event) {
        this.showInput = column.property + row.resultId;
        this.oldData[column.property] = row[column.property];
      },
      async exportData() {
        const res = await this.$http({
          url: `/purchase/purchaseacceptresult/exportData`,
          method: 'get',
          responseType: 'blob',
          params: this.$http.adornParams()
        });
        downLoadFile(res)
      }
    },
    directives: {
      // 通过自定义指令实现的表单自动获得光标的操作
      focus: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "input") {
            el.focus()
          } else {
            if (el.getElementsByTagName("input")) {
              el.getElementsByTagName("input")[0].focus()
            }
          }
          el.focus()
        }
      },
      focusTextarea: {
        inserted: function(el) {
          if (el.tagName.toLocaleLowerCase() == "textarea") {
            el.focus()
          } else {
            if (el.getElementsByTagName("textarea")) {
              el.getElementsByTagName("textarea")[0].focus()
            }
          }
          el.focus()
        }
      }
    },
  }
</script>
